package com.yiqi.liebang.feature.home.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.baiiu.filter.ClassifyBo;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.PrizeBo;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.entity.enums.OrderType;
import com.yiqi.liebang.feature.home.contract.ClassifyContract;
import com.yiqi.liebang.feature.home.dependencies.classsify.ClassifyPresenterModule;
import com.yiqi.liebang.feature.home.dependencies.classsify.DaggerClassifyComponent;
import com.yiqi.liebang.feature.setting.view.ServiceAgreementActivity;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class AskActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, ClassifyContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_ask_content)
    EditText mEdtAskContent;
    @BindView(R.id.cbx_lottery_daleto)
    CheckBox mCbxLotteryDaleto;
    @BindView(R.id.tv_ask_text_count)
    TextView mTvAskTextCount;
    @BindView(R.id.btn_to_xieyi)
    TextView mBtnToXieyi;
    private Double mPrize;
    private boolean isNext = true;

    private String mUserUid;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_ask;
    }
    @Inject
    ClassifyContract.Presenter mPresenter;
    @Override
    protected void initInjector() {
        DaggerClassifyComponent.builder().classifyPresenterModule(new ClassifyPresenterModule(this)).build().inject(this);
    }


    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_next);
        initToolBar(mToolbar, "", true, true);
        mTvAskTextCount.setText(Html.fromHtml("<font color=\"#555555\">0</font>/500"));
        mUserUid = this.getIntent().getStringExtra("user_id");
    }

    @Override
    protected void initData() {
        AM.api().createApiService(HomeApi.class).getQuestionPrize()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<PrizeBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PrizeBo prizeBo) {
                        mPrize = prizeBo.getPricing();
                        initToolBar(mToolbar, Html.fromHtml("付费<font color=\"#00ADEF\">" + mPrize + "元</font>向行家提问"),
                                true, true);
                    }

                    @Override
                    public void onError(Throwable e) {
                        showMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(this);
        mEdtAskContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mTvAskTextCount.setText(Html.fromHtml("<font color=\"#555555\">" + s.toString().length() + "</font>/500"));
            }
        });


        mCbxLotteryDaleto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isNext = isChecked;
            }
        });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        if (item.getItemId() == R.id.action_next) {

            if (!isNext) {
                T.showShort("请同意猎帮服务协议！");
                return true;
            }
            if (TextUtils.isEmpty(mEdtAskContent.getText().toString())) {
                T.showShort("请输入问题描述！");
                return true;
            }
            if (mEdtAskContent.getText().toString().length() < 10) {
                T.showShort("问题描述不能少于10个字！");
                return true;
            }

            if (mPrize==null) {
                T.showShort("问答价格有误！");
                return true;
            }
            if (TextUtils.isEmpty(mUserUid)) {
                Intent intent = new Intent(AskActivity.this, ChoiceExpertActivity.class);
                intent.putExtra("ask_prize", mPrize);
                intent.putExtra("user_id", mUserUid);
                intent.putExtra("ask_content", mEdtAskContent.getText().toString());
                startActivity(intent);
            }else {
               mPresenter.getUserLabelByType(mUserUid);
            }
        }
        return false;
    }


    @OnClick(R.id.btn_to_xieyi)
    public void onViewClicked() {
        startActivity(new Intent(this, ServiceAgreementActivity.class));
    }

    @Override
    public void showUserSuccess(String classifyBos) {
        Intent intent = new Intent(this, PayActivity.class);
        intent.putExtra("ask_prize", mPrize);
        intent.putExtra("ask_userid", mUserUid);
        intent.putExtra("ask_content", mEdtAskContent.getText().toString());
        intent.putExtra("ask_lables", classifyBos);
        intent.putExtra("order_type", OrderType.WENDA.getIndex());
        startActivity(intent);
        finish();
    }

    @Override
    public void showUserError(String classifyBos) {
      showMsg(classifyBos);
    }

    @Override
    public void showSuccess(List<ClassifyBo> classifyBos) {

    }

    @Override
    public void showExpert(List<SearchFriendBo.UserFriendBean> classifyBos) {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void showError(String ex) {

    }
}
